// javaScript的date有几个要注意的：
// 1. date.GetMonth() == 1 表示2月
// 2. date.GetDay() == 0 是礼拜天
// 3. date.toLocalString 显示上午12:00实际是0:00

// 获得这个月有多少天
// 相当于下个月的0号（ = 该月的最后一天 ）的天数！
function getMonthDay(year: number, month: number): number {
	const days = new Date(year, month + 2, 0).getDate();
	return days;
}

// 获得本周的该月份的第几周 from stackoverflow
// 按照中国的习惯把周一当作每周第一天
function getWeekInMonth(year: number, month: number, day: number): number {
	let weekNum = 1; // we start at week 1

	let weekDay = new Date(year, month - 1, 1).getDay(); // we get the weekDay of day 1
	// we recalculate the weekDay (Mon:0, Tue:1, Wed:2, Thu:3, Fri:4, Sat:5, Sun:6)
	weekDay = weekDay === 0 ? 6 : weekDay - 1;

	let monday = 1 + (7 - weekDay); // we get the first monday of the month

	while (monday <= day) {
		// we calculate in wich week is our day
		weekNum++;
		monday += 7;
	}

	return weekNum; // we return it
}

function getWeekInMonthDate(date: Date): number {
	return getWeekInMonth(date.getFullYear(), date.getMonth() + 1, date.getDate());
}

const DAY_SECOND = 24 * 3600;
const oneMin = 60 * 1000;
const oneHour = 3600 * 1000; // 毫秒
const oneDay = oneHour * 24; // 毫秒
const oneWeek = oneDay * 7;

// 12:00 => [12,0,0]
function parseTimeHourMin(time: string): number[] {
	const pos = time.indexOf(':');
	if (pos < 0) return [0, 0, 0];
	const sp = time.split(':');
	return [Number(sp[0]), Number(sp[1]), Number(sp[2] || 0)];
}

// 2018.11.19 => [2018,11,19]
function parseTimeYearMonthDay(time: string): number[] {
	const s = time.split('.');
	if (s.length === 3) return [Number(s[0]), Number(s[1]), Number(s[2])];
	if (s.length === 2) {
		const date = new Date();
		return [date.getFullYear(), Number(s[0]), Number(s[1])];
	}
	return [2019, 1, 1];
}

// 2018.11.19 12:00 or 2018.11.19 => [2018,11,19,12,0]
function parseTimeFullHourMin(time: string): number[] {
	const pos = time.indexOf(' ');
	// console.log("ParseTimeFullHourMin",time,pos)
	if (pos < 0) {
		const [y, m, d] = parseTimeYearMonthDay(time);
		return [y, m, d, 0, 0, 0];
	}
	const l = time.substr(0, pos);
	const [y, m, d] = parseTimeYearMonthDay(l);
	const r = time.substr(pos + 1);
	const [hour, min, sec] = parseTimeHourMin(r);
	// console.log("ParseTimeFullHourMin",time,[y,m,d],[hour,min,sec])
	return [y, m, d, hour, min, sec];
}

// (s)3661 => '01:01:01'
function time2HourMinSec(time: number): string {
	if (time < 0) {
		time = 0;
	}
	if (!time) {
		return '00:00:00';
	}
	let h = (Math.floor(time / 3600) % 24).toString();
	let m = (Math.floor(time / 60) % 60).toString();
	let s = Math.floor(time % 60).toString();
	if (h.length <= 1) {
		h = `0${h}`;
	}
	if (m.length <= 1) {
		m = `0${m}`;
	}
	if (s.length <= 1) {
		s = `0${s}`;
	}
	return `${h}:${m}:${s}`;
}


function time2MinSec(time: number): string {
	if (time < 0) {
		time = 0;
	}
	if (!time) {
		return '00:00';
	}
	let m = (Math.floor(time / 60)).toString();
	let s = Math.floor(time % 60).toString();
	if (m.length <= 1) {
		m = `0${m}`;
	}
	if (s.length <= 1) {
		s = `0${s}`;
	}
	return `${m}:${s}`;
}



function time2HourMin(time: number): string {
	if (!time) {
		return '00:00';
	}
	let h = (Math.floor(time / 3600) % 24).toString();
	let m = (Math.floor(time / 60) % 60).toString();
	if (h.length <= 1) {
		h = `0${h}`;
	}
	if (m.length <= 1) {
		m = `0${m}`;
	}
	return `${h}:${m}`;
}

function getDateByString(time: string): Date {
	const [_y, m, d, hour, min, sec] = parseTimeFullHourMin(time);
	let y = _y;
	if (y === 0) y = new Date().getFullYear();
	return new Date(y, m - 1, d, hour, min, sec);
}

// 获得从2000,1,1 到date的绝对天数！
function getAbsDay2000(date: Date): number {
	const start = new Date(2000, 0, 0);
	const diff = date.getTime() - start.getTime();
	const day = Math.floor(diff / oneDay);
	// console.log('Day of year: ' + day);
	return day;
}

// '2020-5-31 23:47:32'
function getTimeStr(ms: number): string {
	const d = new Date(ms);
	return d.toLocaleString();
}

function getSunday(date: Date): Date {
	// 用中国的计算方法来计算周，周日是本周的最后一天！
	// 相当于sunday不变
	if (date.getDay() === 0) return date;
	return new Date(date.getTime() + (7 - date.getDay()) * DAY_SECOND * 1000);
}

// time same
function isSameYear(date1: Date, date2: Date): boolean {
	return date1.getFullYear() === date2.getFullYear();
}
function isSameMonth(date1: Date, date2: Date): boolean {
	return date1.getMonth() === date2.getMonth() && isSameYear(date1, date2);
}
function isSameDay(date1: Date, date2: Date): boolean {
	return date1.getDate() === date2.getDate() && isSameMonth(date1, date2);
}
function isSameWeek(date1: Date, date2: Date): boolean {
	const s1 = getSunday(date1);
	const s2 = getSunday(date2);
	return isSameDay(s1, s2);
}

function timeGreat(time: string): boolean {
	return new Date() > new Date(time);
}

// time diff
function timeDiffDay(date1: Date, date2: Date): number {
	const d1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
	const d2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
	return (d1.getTime() - d2.getTime()) / oneDay;
}

function timeDiffHour(date1: Date, date2: Date): number {
	const d1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate(), date1.getHours());
	const d2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate(), date2.getHours());
	return (d1.getTime() - d2.getTime()) / oneHour;
}

function timeDiffMin(date1: Date, date2: Date): number {
	const d1 = new Date(
		date1.getFullYear(),
		date1.getMonth(),
		date1.getDate(),
		date1.getHours(),
		date1.getMinutes(),
	);
	const d2 = new Date(
		date2.getFullYear(),
		date2.getMonth(),
		date2.getDate(),
		date2.getHours(),
		date2.getMinutes(),
	);
	return (d2.getTime() - d1.getTime()) / oneMin;
}

function timeDiffWeek(date1: Date, date2: Date): number {
	return timeDiffDay(getSunday(date1), getSunday(date2)) / 7;
}

function timeDiffYear(date1: Date, date2: Date): number {
	return date1.getFullYear() - date2.getFullYear();
}

function timeDiffMonth(date1: Date, date2: Date): number {
	return timeDiffYear(date1, date2) * 12 + date1.getMonth() - date2.getMonth();
}

function getTimeDesc(key: string, hour: number): string {
	if (key === '昼夜') {
		if (hour >= 7 && hour <= 7 + 12) {
			return '白天';
		}
		return '夜晚';
	}
	return '';
}
